package com.laizhenghua.wxstub.controller;

import com.alibaba.fastjson.JSONObject;
import com.laizhenghua.wxstub.service.LoginService;
import com.laizhenghua.wxstub.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: laizhenghua
 * @date: 2022/9/8 20:25
 */
@Api(tags = "APP-登录(获取token)")
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ApiOperation(value = "登录获取token", notes = "/login", response = String.class)
    public R login(@RequestBody JSONObject loginInfo) {
        String loginId = loginInfo.getString("loginId");
        String password = loginInfo.getString("password");
        if (loginId == null || password == null) {
            return R.error(500, "登录信息错误！");
        }
        String token = loginService.login(loginId, password);
        if (token == null) {
            return R.error(500, "认证失败！");
        }
        return R.ok().put("token", token);
    }
}
